//
//  HELogisticsViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/13.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HELogisticsViewController.h"
#import "HELogisticModel.h"
#import "HELogisticsCell.h"

@interface HELogisticsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) HELogisticModel *details;
@property (nonatomic, strong) UILabel *statusLb;
@property (nonatomic, strong) UILabel *logisticsLb;

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HETraces *> * dataSourceArr;

@property (nonatomic, assign) BOOL isFinish;

@end

@implementation HELogisticsViewController

- (NSMutableArray<HETraces *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"物流信息";
    
    self.view.backgroundColor = CViewBgColor;
    
    [self.view addSubview:self.tableView];
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    
    [self initUI];
    [self loadData];
}

- (void)initUI {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, 75)];
    headView.backgroundColor = KWhiteColor;
    [self.view addSubview:headView];
    
    UIImageView *logo = [UIImageView new];
    logo.image = IMAGE_NAMED(@"track_info");
    logo.clipsToBounds = YES;
    [headView addSubview:logo];
    
    // 物流状态
    UILabel *statusLb = [UILabel new];
    statusLb.text = @"-";
    statusLb.font = DYFont(18);
    statusLb.textColor = CNavBgColor;
    [headView addSubview:statusLb];
    _statusLb = statusLb;
    
    // 快递号
    UILabel *logisticsLb = [UILabel new];
    logisticsLb.text = @"-";
    logisticsLb.font = DYFont(18);
    logisticsLb.textColor = CNavLightBlackColor;
    logisticsLb.numberOfLines = 0;
    [headView addSubview:logisticsLb];
    _logisticsLb = logisticsLb;
    
    [logo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView);
        make.left.equalTo(headView).mas_offset(15);
        make.width.mas_offset(40);
        make.height.mas_offset(32);
    }];
    
    [statusLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView).mas_offset(15);
        make.left.equalTo(logo.mas_right).mas_offset(15);
        make.right.equalTo(headView).mas_offset(-15);
    }];
    
    [logisticsLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(statusLb.mas_bottom).mas_offset(5);
        make.left.equalTo(logo.mas_right).mas_offset(15);
        make.right.equalTo(headView).mas_offset(-15);
    }];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight + 75 + 10 , KScreenWidth, KScreenHeight - (ZXNavBarHeight + 75 + 10)) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = YES;
            [weakSelf loadData];
        }];

        [_tableView registerClass:[HELogisticsCell class] forCellReuseIdentifier: @"HELogisticsCell"];
    }
    return _tableView;
}

#pragma mark - loadData
- (void)loadData {
    [WOAFNetWorkingHandle POSTWithBaseUrlByKeyValue:KServerUrl suffixUrl:Klogist parameters:@{@"orderTraces": _order.deliverySn} success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }

        self.details = [HELogisticModel mj_objectWithKeyValues:response.data];

        if ([@"3" isEqualToString:self.details.State]) {
            self.isFinish = YES;
        }
        
        [self.dataSourceArr addObjectsFromArray:[[self.details.Traces reverseObjectEnumerator] allObjects]];

        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];

        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }
        
        [self setupData];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)setupData {
    _statusLb.text = [HEEnums logisticStatusToText: _details.State];
    _logisticsLb.text = [NSString stringWithFormat:@"%@ %@", _details.ShipperName, _details.LogisticCode];
}

 
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HELogisticsCell *cell = [_tableView dequeueReusableCellWithIdentifier: @"HELogisticsCell"];
    
    if (_isFinish && indexPath.row == 0) {
        [cell finishStateView];
    }else {
        [cell normalViwe];
    }
    
    cell.model = _dataSourceArr[indexPath.row];
     return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(HELogisticsCell.class) cacheByIndexPath:indexPath configuration:^(HELogisticsCell *cell) {
        cell.model = _dataSourceArr[indexPath.row];
    }];
}

@end
